﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Activity.Data;
using Activity.Helper;
using Nancy.Extensions;

namespace Activity.API
{
    public class TaskReportHandler
    {
        public IList<TaskReportModel> GetTaskReport(string Display,string StartDateStr,string EndDateStr)
        {
            try
            {
                var sql = $@"SELECT  s.TaskType ,
        ISNULL(ShopCount, 0) AS ShopCount ,
        ISNULL(ReadCount, 0) AS ReadCount
FROM    dbo.t_task_set s
        LEFT JOIN ( SELECT  TaskType ,
                            COUNT(1) AS ShopCount
                    FROM    ( SELECT DISTINCT
                                        tsp.Display ,
                                        TaskType ,
                                        tsp.ShopCode ,
                                        fin.IsRead
                              FROM      t_task_shop_permissions tsp
                                        INNER JOIN V_UserShopDataRights s ON tsp.ShopCode = s.shopcode
                                        INNER JOIN dbo.t_task_shoplist sl ON s.shopid = sl.ShopID
                                        INNER JOIN dbo.t_task_set ts ON sl.TaskID = ts.TaskID
                                        INNER JOIN dbo.t_task_detail_finish fin ON ts.TaskID = fin.TaskID
                                                              AND s.userid = fin.ProcessingCreator
                              WHERE     Display = '{Display}'
                                        {(string.IsNullOrEmpty(StartDateStr) ? "" : $"AND ProcessingTime >= '{Convert.ToDateTime(StartDateStr)}'")}
                                        {(string.IsNullOrEmpty(EndDateStr) ? "" : $"AND ProcessingTime <= '{Convert.ToDateTime(EndDateStr).AddDays(1).AddMilliseconds(-1)}'")}
                            ) table1
                    GROUP BY TaskType
                  ) table1 ON s.TaskType = table1.TaskType
        LEFT JOIN ( SELECT  TaskType ,
                            COUNT(1) AS ReadCount
                    FROM    ( SELECT DISTINCT
                                        tsp.Display ,
                                        TaskType ,
                                        tsp.ShopCode ,
                                        fin.IsRead
                              FROM      t_task_shop_permissions tsp
                                        INNER JOIN V_UserShopDataRights s ON tsp.ShopCode = s.shopcode
                                        INNER JOIN dbo.t_task_shoplist sl ON s.shopid = sl.ShopID
                                        INNER JOIN dbo.t_task_set ts ON sl.TaskID = ts.TaskID
                                        INNER JOIN dbo.t_task_detail_finish fin ON ts.TaskID = fin.TaskID
                                                              AND s.userid = fin.ProcessingCreator
                              WHERE     Display = '{Display}'
                                        {(string.IsNullOrEmpty(StartDateStr) ? "" : $"AND ProcessingTime >= '{Convert.ToDateTime(StartDateStr)}'")}
                                        {(string.IsNullOrEmpty(EndDateStr) ? "" : $"AND ProcessingTime <= '{Convert.ToDateTime(EndDateStr).AddDays(1).AddMilliseconds(-1)}'")}
                                        AND IsRead = 1
                            ) table1
                    GROUP BY TaskType
                  ) table2 ON s.TaskType = table2.TaskType
ORDER BY s.TaskType";
                var db = new ActivityEntities();
                var query = db.Database.SqlQuery<TaskReportDBModel>(sql);
                var json = JsonHelper.GetFileJson("~/ShopTask/TaskType.json");
                var type = json.FromJson<TaskModel>();
                var result = new List<TaskReportModel>();
                foreach (var item in query)
                {
                    var taskType = type.Task.First(d => d.Value == item.TaskType).TaskType;
                    var newItem = new TaskReportModel
                    {
                        TaskType = taskType,
                        Sum = item.ShopCount,
                        ReadSum = item.ReadCount,
                    };
                    result.Add(newItem);
                }
                return result;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return null;
            }
        }
    }

    public class TaskReportModel
    {
        public int Sum { get; set; }
        public int ReadSum { get; set; }
        public string TaskType { get; set; }
    }

    public class TaskReportDBModel
    {
        public int ShopCount { get; set; }
        public int ReadCount { get; set; }
        public int TaskType { get; set; }
    }

    public class TaskModel
    {
        public IList<TaskTypeModel> Task { get; set; }
    }

    public class TaskTypeModel
    {
        public string TaskType { get; set; }
        public int Value { get; set; }
    }
}
